package org.jumiz.dbview.own.service;

import org.jumiz.dbview.own.entity.ConnectEntity;
import org.jumiz.dbview.own.repository.ConnectRepository;
import org.jumiz.dbview.own.vo.SubMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

@Service
public class ConnectService {

    @Autowired
    private ConnectRepository connectRepository;

    public Flux<ConnectEntity> getAll() {
        return connectRepository.findAll();
    }

    public Mono<ConnectEntity> save(ConnectEntity entity) {
        return connectRepository.save(entity);
    }

    public Mono<Boolean> testConnect(ConnectEntity entity) {
        return DatabasesServiceFactory.getBean(entity.getDbType()).testConnection(entity);
    }

    public Mono<List<SubMenuVo>> getMenusData(Long id) {
        return connectRepository.findById(id).map(item -> DatabasesServiceFactory.getBean(item.getDbType()).getMenusData(item));
    }
}
